package com.librarysys.controller;

import com.librarysys.common.Const;
import com.librarysys.common.Result;
import com.librarysys.dao.PurviewMapper;
import com.librarysys.entity.Booktype;
import com.librarysys.entity.Manager;
import com.librarysys.entity.Purview;
import com.librarysys.service.IBookService;
import com.librarysys.vo.BookDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by yuhui on 2017/7/12.
 */
@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    private PurviewMapper purviewMapper;

    @Autowired
    private IBookService iBookService;

    @RequestMapping("/type/list")
    public String listBookType(HttpServletRequest request) {
        Boolean comfirm = comfirm(request.getSession());
        if (!comfirm) {
            return "no_access";
        } else {
            Result result = iBookService.listBookType();
            request.setAttribute("types", result.getData());
            return "bookType";
        }
    }

    @RequestMapping("/type/add")
    public String addBookType(Booktype booktype, HttpServletRequest request) {
        Boolean comfirm = comfirm(request.getSession());
        if (!comfirm) {
            return "no_access";
        } else {
            Result result = iBookService.addBookType(booktype);
            request.setAttribute("msg", result.getMsg());
            return "/book/type/list";
        }
    }

    @RequestMapping("/type/del")
    public String delBookType(Integer id, HttpServletRequest request) {
        Boolean comfirm = comfirm(request.getSession());
        if (!comfirm) {
            return "no_access";
        } else {
            Result result = iBookService.delBookType(id);
            request.setAttribute("msg", result.getMsg());
            return "/book/type/list";
        }
    }

    @RequestMapping("/list")
    public String listBook(HttpServletRequest request) {
        Boolean comfirm = comfirm(request.getSession());
        if (!comfirm) {
            return "no_access";
        } else {
            Result result = iBookService.listBook();
            request.setAttribute("books", result.getData());
            return "book";
        }
    }

    @RequestMapping("/add")
    public String addBook(Booktype booktype, HttpServletRequest request) {
        Boolean comfirm = comfirm(request.getSession());
        if (!comfirm) {
            return "no_access";
        } else {
            Result result = iBookService.addBookType(booktype);
            request.setAttribute("msg", result.getMsg());
            return "/book/list";
        }
    }

    @RequestMapping("/del")
    public String delBook(Integer id, HttpServletRequest request) {
        Boolean comfirm = comfirm(request.getSession());
        if (!comfirm) {
            return "no_access";
        } else {
            Result result = iBookService.delBook(id);
            request.setAttribute("msg", result.getMsg());
            return "/book/list";
        }
    }

    @RequestMapping("/query")
    public String queryBook(HttpServletRequest request) {
        Boolean comfirm = comfirm(request.getSession());
        if (!comfirm) {
            return "no_access";
        } else {
            Result result = iBookService.listBook();
            request.setAttribute("books", result.getData());
            return "bookQuery";
        }
    }

    @RequestMapping("/query/do_query")
    public String doQueryBook(HttpServletRequest request, BookDetailVO bookDetailVO) {
        Boolean comfirm = comfirm(request.getSession());
        if (!comfirm) {
            return "no_access";
        } else {
            Result result = iBookService.doQueryBook(bookDetailVO);
            request.setAttribute("books", result.getData());
            return "bookQuery";
        }
    }

    private Boolean comfirm(HttpSession session) {
        Manager manager = (Manager) session.getAttribute(Const.CURRENT_USER);
        Purview purview = purviewMapper.selectByPrimaryKey(manager.getId());
        if (purview == null) {
            return false;
        }
        if (purview.getBookset()) {
            return true;
        }
        return false;
    }
}
